﻿// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved.
// See License.txt in the project root for license information.
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace ActorFramework.Fakebook
{
    /// <summary>
    /// Interaction logic for PictureWindow.xaml
    /// </summary>
    public partial class PictureWindow : Window
    {
        public PictureWindow(PictureInfo picture)
        {
            InitializeComponent();

            // Display the image
            BitmapImage bi = new BitmapImage();
            bi.BeginInit();
            bi.StreamSource = new MemoryStream(picture.ImageBytes);
            bi.EndInit();
            ImageCanvas.Source = bi;

            // Set the author name
            AuthorTextBlock.Text = picture.Author ?? "<unknown>";

            // Display the original URI (note: local files added from disk won't have a URI)
            if (picture.OriginalUri != null)
            {
                Hyperlink photoLink = new Hyperlink(new Run(picture.Title));
                photoLink.NavigateUri = new Uri(picture.OriginalUri);
                photoLink.RequestNavigate += Hyperlink_RequestNavigate;
                OriginalUriTextBlock.Inlines.Add(photoLink);
            }
            else
            {
                OriginalUriTextBlock.Text = picture.Title;
                OriginalUriLabel.Content = "Original file:";
            }
            
            // Display the license
            if (picture.LicenseName != null)
            {
                if (picture.LicenseUri != null)
                {
                    String friendlyName = picture.LicenseName;
                    if (friendlyName.StartsWith("CC "))
                        friendlyName = "Creative Commons " + friendlyName.Substring(3);
                    Hyperlink licenseLink = new Hyperlink(new Run(friendlyName));
                    licenseLink.NavigateUri = new Uri(picture.LicenseUri);
                    licenseLink.RequestNavigate += Hyperlink_RequestNavigate;
                    LicenseTextBlock.Inlines.Add(licenseLink);
                }
                else
                    LicenseTextBlock.Inlines.Add(picture.LicenseName);
            }
            else
                LicenseTextBlock.Text = "<none>";
        }

        // For clicking on URI's in TextBlocks
        private void Hyperlink_RequestNavigate(object sender, System.Windows.Navigation.RequestNavigateEventArgs e)
        {
            System.Diagnostics.Process.Start(e.Uri.AbsoluteUri);
        }
    }
}
